/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.util.HashMap;
import java.util.Map;

/**
 * This class maps one instance of postA to an instance of postB
 *
 * @author Anton Strack
 */
public class RelatedMap {

    /**
     * The id of the specific relationship.
     */
    private int id;
    /**
     * the post id identifies the postA of the relationship map
     */
    private int postAId;
    /**
     * the post id identifies the postB of the relationship map
     */
    private int postBId;
    /**
     * The id the indicates the relationship between postA and postB
     */
    private int relationshipTypeId;
    /**
     * This map is used to store runtime information that isn't stored in the
     * primary database row.
     */
    protected Map<String, String> derivedAttributes;

    /**
     * Construct the related map object
     */
    public RelatedMap() {
        this.derivedAttributes = new HashMap<String, String>();
    }

    /**
     * @return the postAId
     */
    public int getPostAId() {
        return postAId;
    }

    /**
     * @param postAId the postAId to set
     */
    public void setPostAId(int postAId) {
        this.postAId = postAId;
    }

    /**
     * @return the postBId
     */
    public int getPostBId() {
        return postBId;
    }

    /**
     * @param postBId the postBId to set
     */
    public void setPostBId(int postBId) {
        this.postBId = postBId;
    }

    /**
     * @return the relationshipTypeId
     */
    public int getRelationshipTypeId() {
        return relationshipTypeId;
    }

    /**
     * @param relationshipTypeId the relationshipTypeId to set
     */
    public void setRelationshipTypeId(int relationshipTypeId) {
        this.relationshipTypeId = relationshipTypeId;
    }

    /**
     * Get the derived string value matching the key. Derived values are values
     * not stored in the bean's database table. They are either the matching
     * names of id values stored in the bean table or they are other related
     * values.
     *
     * @param key Column name key for retrieving the derived attribute. As long
     * as setDerivedValue() was used to set the derived value, case does not
     * matter for the key.
     * @return the derived attribute value matching the key or an empty string
     * if the key doesn't exist.
     */
    public String getDerivedValue(String key) {
        key = key.toLowerCase();
        if (this.derivedAttributes.containsKey(key)) {
            return this.derivedAttributes.get(key);
        }
        return "";
    }

    /**
     * Get the derived int value matching the key. Derived values are values not
     * stored in the bean's database table. They are either the matching names
     * of id values stored in the bean table or they are other related values.
     * This method attempts to return the derived value as an integer.
     *
     * @param key Column name key for retrieving the derived attribute. As long
     * as setDerivedValue() was used to set the derived value, case does not
     * matter for the key.
     * @return the derived attribute value matching the key or 0 if the key
     * doesn't exist
     */
    public int getDerivedInt(String key) {
        String value;
        key = key.toLowerCase();
        if (this.derivedAttributes.containsKey(key)) {
            value = this.derivedAttributes.get(key);
            if (value != null && value.isEmpty() == false) {
                Integer i = Integer.parseInt(value);
                return i.intValue();
            }
        }
        // return zero if any above condition fails
        return 0;
    }

    /**
     * Set the name of the related id value.
     *
     * @param nameFor the key name used to get the value from the bean.
     * Generally follows the pattern StateName if the bean has a field StateId.
     * @param name the name that is related to the id value
     */
    public void setDerivedValue(String nameFor, String name) {
        // convert the name to lowercase to reduce bugs caused by incorrect case.
        this.derivedAttributes.put(nameFor.toLowerCase(), name);
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
}
